package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.DemandCategory;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.DemandCategoryDao;
import com.weipai.springboot.module.mapper.DemandCategoryMapper;
import com.weipai.springboot.module.service.DemandCategoryService;
import com.weipai.springboot.module.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: 曹朋飞
 * @Date: 2018/07/27 17:50
 */
@Service
public class DemandCategoryServiceImpl implements DemandCategoryService {

    private static final Logger log = LoggerFactory.getLogger(DemandCategoryServiceImpl.class);

    @Autowired
    DemandCategoryDao demandCategoryDao;
    
    //保存
    @Override
    public Response save(DemandCategory demandCategory) {
        Response response = new Response();
        if(1==demandCategoryDao.insert(demandCategory)){
            response.setCode(RetCode.SUCCESS);
        }else{
            log.debug("保存需求类别失败:{}",demandCategory.getDemandCategoryName());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //修改
    @Override
    public Response update(DemandCategory demandCategory) {
        Response response = new Response();
        demandCategory.setUpdateTime(DateUtils.timeFormat(System.currentTimeMillis()));
        if(1==demandCategoryDao.updateByPrimaryKey(demandCategory)){
            response.setCode(RetCode.SUCCESS);
        }else{
            log.debug("修改需求类别失败:{}",demandCategory.getDemandCategoryName());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //查询需求类别详情
    @Override
    public Response getDemandCategoryById(String demandCategoryId) {
        Response response = new Response();
        DemandCategory demandCategory = demandCategoryDao.getDemandCategoryById(demandCategoryId);
        if(demandCategory!=null){
            response.setCode(RetCode.SUCCESS);
            response.put("data",demandCategory);
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //批量删除
    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        if(demandCategoryDao.batchDeleteById(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }


    //分页查询
    @Override
    public List<DemandCategory> selectAllByDemandCategoryDel(int demandCategoryDel,
                                                             String search, String sort, String way) {
        return demandCategoryDao.selectAllByDemandCategoryDel(demandCategoryDel,search, sort, way);
    }



    //删除
    @Override
    public Response deleteById(String demandCategoryId) {
        Response response = new Response();
        DemandCategory demandCategory = demandCategoryDao.getDemandCategoryById(demandCategoryId);
        if(null!=demandCategory){
            demandCategory.setDelTime(DateUtils.timeFormat(System.currentTimeMillis()));
            demandCategory.setDemandCategoryDel(-1);
            if(1==demandCategoryDao.updateByPrimaryKey(demandCategory)){
                response.setCode(RetCode.SUCCESS);
            }else{
                response.setCode(RetCode.REQUEST_ERROR);
            }
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }



    //更改需求类别的状态
    @Override
    public Response changeStatus(String demandCategoryId, Integer status) {
        Response response = new Response();
        if(1==demandCategoryDao.updateStatus(demandCategoryId,status)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }
}
